//
//  ContactUsVC.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit


final class ContactUsVC: UIViewController {
    
    private lazy var backBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "cancel"), for: .normal)
        btn.addTarget(self, action: #selector(goBack(sender:)), for: .touchUpInside)
        return btn
    }()
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.sizeToFit()
        var paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = 1.26
        
        let attrs = [
            NSAttributedString.Key.kern: 0.16,
            NSAttributedString.Key.paragraphStyle: paragraphStyle,
            NSAttributedString.Key.foregroundColor: UIColor.darkBlueColor,
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .bold)
            ] as [NSAttributedString.Key : Any]
        lbl.numberOfLines = 0
        lbl.attributedText = NSMutableAttributedString(string: "Возникли вопросы или есть предложение?", attributes: attrs)
        return lbl
    }()
    private lazy var hintLbl: UILabel = {
        let lbl = UILabel()
        lbl.text = "Свяжитесь с нами по\nWhatsapp, Telegram"
        lbl.textColor = .rgb(60,72,88)
        lbl.font = .systemFont(ofSize: 15, weight: .semibold)
        lbl.numberOfLines = 0
        lbl.sizeToFit()
        return lbl
    }()
    private lazy var bannerImg: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.image = UIImage(named: "chat_banner")
        return imgView
    }()
    private lazy var whatsAppBtn: SocialMediaButton = {
        let button = SocialMediaButton(media: .whatsApp("WhatsApp", 1, UIImage(named: "whatsapp"), .rgb(85,203,109)))
        return button
    }()
    private lazy var telegramBtn: SocialMediaButton = {
        let button = SocialMediaButton(media: .telegram("Telegram", 2, UIImage(named: "telegram"), .rgb(59,152,222)))
        return button
    }()
    
    @objc private func goBack(sender: UIButton) {
        navigationController?.popViewController(animated: true)
    }
    
    @objc private func openSocialMedia (sender: GestureWithTag) {
        if sender.tag == 1 {
            self.openLink("https://api.whatsapp.com/send?phone=+77470726323")
        } else {
            self.openLink("https://t.me/undefinedisnotanobject")
        }
    }
    
    private func openLink (_ link: String) {
        if UIApplication.shared.canOpenURL(URL(string: link)!) {
            UIApplication.shared.openURL(URL(string: link)!)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = backColor
        self.addViews()
    }

    private let backColor = UIColor.rgb(249,250,252)
    
    private func addViews () {
        view.addSubview(backBtn)
        view.addSubview(titleLbl)
        view.addSubview(hintLbl)
        view.addSubview(bannerImg)
        view.addSubview(whatsAppBtn)
        view.addSubview(telegramBtn)
        
        let whGesture = GestureWithTag(target: self, action: #selector(openSocialMedia(sender:)))
        whGesture.tag = 1
        whatsAppBtn.addGestureRecognizer(whGesture)
        
        let telegramGesture = GestureWithTag(target: self, action: #selector(openSocialMedia(sender:)))
        telegramGesture.tag = 2
        telegramBtn.addGestureRecognizer(telegramGesture)
        
        self.addConstraints()
    }
    
    private func addConstraints () {
        backBtn.snp.makeConstraints { (make) in
            make.width.equalTo(12.5)
            make.height.equalTo(20.5)
            make.leading.equalTo(16)
            make.top.equalToSuperview().offset(40)
        }
        titleLbl.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(78)
            make.leading.equalToSuperview().inset(16)
            make.trailing.equalToSuperview().offset(-35)
        }
        hintLbl.snp.makeConstraints { (make) in
            make.top.equalTo(titleLbl.snp.bottom).offset(20)
            make.leading.trailing.equalToSuperview().inset(16)
        }
        telegramBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-20)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(44)
        }
        whatsAppBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(telegramBtn.snp.top).offset(-12)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(44)
        }
        bannerImg.snp.makeConstraints { (make) in
            make.top.equalTo(hintLbl.snp.bottom).offset(48)
            make.bottom.equalTo(whatsAppBtn.snp.top).offset(-80)
            make.leading.trailing.equalToSuperview().inset(37)
        }
    }

}


final class SocialMediaButton: UIView {
    
    enum SocialMedia{
        case whatsApp (String, Int, UIImage?, UIColor)
        case telegram (String, Int, UIImage?, UIColor)
    }
    
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 17, weight: .medium)
        lbl.textColor = .white
        return lbl
    }()
    
    private lazy var rowStack: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.alignment = .center
        stack.distribution = .equalSpacing
        stack.spacing = 12
        return stack
    }()
    
    
    
    init (media: SocialMedia) {
        super.init(frame:.zero)
        self.addViews()
        
        switch media {
        case .telegram(let name, _, let icon, let backColor):
            self.titleLbl.text = "Написать в " + name
            self.iconView.image = icon
            self.backgroundColor = backColor
        case .whatsApp(let name, _, let icon, let backColor):
            self.titleLbl.text = "Написать в " + name
            self.iconView.image = icon
            self.backgroundColor = backColor
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func addViews () {
        self.addSubview(rowStack)
        self.layer.cornerRadius = 15
        rowStack.addArrangedSubview(iconView)
        rowStack.addArrangedSubview(titleLbl)
        iconView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
        }
        rowStack.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(20)
        }
    }
}
